
<template>
  <a-card :bordered="false" :bodyStyle="{ height: '100%' }">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="直播名称:">
              <a-input
                v-model="keywords"
                placeholder="搜索直播名称(回车搜索)"
                style="width:80%;"
                @pressEnter="$refs.table.refresh(true)"
              >
                <a-tooltip slot="suffix" title="搜索直播名称">
                  <a-icon
                    type="search"
                    style="color: rgba(0,0,0,.45)"
                    @click="$refs.table.refresh(true)"
                  />
                </a-tooltip>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="上课日期">
              <a-range-picker @change="onChange1" style="width:80%;" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="上课老师:">
              <a-select
                v-model="queryParam.teacher_id"
                placeholder="请选择"
                allowClear
                style="width:80%;"
              >
                <a-select-option
                  v-for="(item,index) in teacherList"
                  :key="index"
                  :value="item.id"
                >{{item.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="showSearch">
            <a-col :md="8" :sm="24">
              <a-form-item label="上课状态">
                <a-select
                  placeholder="请选择"
                  allowClear
                  style="width:80%;"
                  v-model="queryParam.status"
                >
                  <a-select-option :value="1">待开课</a-select-option>
                  <a-select-option :value="2">上课中</a-select-option>
                  <a-select-option :value="3">已完成</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
        </a-row>
      </a-form>
    </div>
    <a style="cursor:pointer" v-if="showSearch" @click="showSearch=!showSearch" id="moreButton">
      收起
      <a-icon type="up"></a-icon>
    </a>
    <a style="cursor:pointer" v-if="!showSearch" @click="showSearch=!showSearch" id="moreButton">
      展开
      <a-icon type="down"></a-icon>
    </a>
    <div class="table-operator">
      <a-button type="primary" @click="$router.push({name:'createLives'})">新建直播课</a-button>
      <a-button style="margin-left:10px;">老师直播说明</a-button>
      <a-button style="margin-left:10px;">下载直播客户端</a-button>
    </div>
    <s-table
      ref="table"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      class="user_management_table"
      style="margin-top:20px;"
    >
      <span slot="time" slot-scope="text,record">
        {{record.class_date}}
        <span
          style="margin-left:10px;"
        >{{record.start_time}}~{{record.end_time}}</span>
      </span>
      <span slot="action" slot-scope="text,record">
        <a>查看直播/回放</a>
        <a-divider type="vertical" />
        <a>学员名单</a>
        <a-divider type="vertical" />
        <a>删除</a>
      </span>
    </s-table>
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { livesList } from '@/api/lives'
import { teacherList } from '@/api/teacher'
export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
  },
  data() {
    return {
      showSearch: false,
      teacherList: [],
      // 表头
      columns: [
        {
          title: '直播名称',
          dataIndex: 'name',
          fixed: 'left',
          scopedSlots: { customRender: 'name' },
        },
        {
          title: '直播模式',
          dataIndex: 'type',
          customRender(text) {
            return text == 1 ? '大班直播' : text == 2 ? '大班互动' : text == 3 ? '小班直播' : '-'
          },
        },
        {
          title: '上课时间',
          dataIndex: 'time',
          width: 200,
          scopedSlots: { customRender: 'time' },
        },
        {
          title: '上课老师',
          dataIndex: 'teacher',
          customRender(text) {
            if (text) {
              return text.name
            } else {
              return '-'
            }
          },
        },
        {
          title: '学员人数',
          align: 'center',
          dataIndex: 'student_count',
        },
        {
          title: '课件管理',
          width: 150,
          dataIndex: 'last_follow_up',
          ellipsis: true,
        },
        {
          title: '上课状态',
          width: 150,
          dataIndex: 'next_follow_up',
          ellipsis: true,
        },
        {
          title: '上课数据',
          dataIndex: 'age',
          // scopedSlots: { customRender: 'age' }
        },
        {
          title: '操作',
          //   width: '100px',
          dataIndex: 'action',
          fixed: 'right',
          align: 'right',
          ellipsis: true,
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        let postData = {}
        for (let key in this.queryParam) {
          if (this.queryParam[key]) {
            let nature = key
            postData[nature] = this.queryParam[key]
          }
        }
        return livesList(Object.assign(parameter, this.queryParam, { keywords: this.keywords || undefined })).then(
          (res) => {
            return res
          }
        )
      },
      keywords: undefined,
      queryParam: {
        school_id: this.$store.state.user.school,
      },
    }
  },
  created() {
    teacherList({ pageSize: 999999, school_id: this.$store.state.user.school }).then((res) => {
      this.teacherList = res.data
    })
  },
  methods: {
    onChange1(date,dateString) {
      this.queryParam.start_date = dateString[0]
      this.queryParam.end_date = dateString[1]
      this.$refs.table.refresh()
    },
  },
  watch: {
    queryParam: {
      handler(val) {
        this.$refs.table.refresh()
      },
      deep: true,
    },
  },
}
</script>
<style lang="less" scoped>
#moreButton {
  position: absolute;
  top: 34px;
  right: 20px;
}
</style>
